<?php
namespace app\common\model\base;

use think\Model;
use think\Db;

/*************************************************  
*ClassName:     Auth
*Description:   权限模型
*Others:
 *************************************************/

class Auth extends Model
{
    /**
     * getGroups 获取用户组（角色）列表
     * @xiao karl
     * @DateTime 2016-08-14T15:44:35+0800
     * @param    array  $where
     * @return   array  [error_code, error_msg, data]             
     */
    static public function getGroups($where = [])
    {
        $result = ['error_code' => 0, 'error_msg' => ''];
        $groups_return = Db::name('auth_groups')->where($where)->select();
        if ($groups_return) {
            $result['data'] = $groups_return;
        }else{
            $result['error_code'] = 1;
            $result['error_msg'] = "未找到角色";
        }
        return $result;
    }

    /**
     * getGroupById 通过group_id获取组信息
     * @karl
     * @DateTime 2017-04-06T08:15:23+0800
     * @param    int                   $group_id 组id
     * @return   array                 [error_code, error_msg, data] 
     */
    static public function getGroupById($group_id)
    {
        return self::getGroups(['group_id' => $group_id]);
    }

    /**
     * getGroupsByUid 通过uid获取组信息
     * @xiao
     * @DateTime 2016-08-14T15:44:35+0800
     * @param    int        $uid
     * @return   array  [error_code, error_msg, data]             
     */
    static public function getGroupByUid($uid)
    {
        $result = ['error_code' => 0, 'error_msg' => ''];
        $res = Db::name('auth_groups_access')->alias('a')->join('ys_auth_groups g','a.group_id = g.group_id','left')->where(['uid' => $uid])->find();
        if ($res) {
            $result['data'] = $res;
        } else {
            $result['error_code'] = 1;
            $result['error_msg'] = "没有得到组信息详情";
        }
        return $result;
    }

    /**
     * getGroupIdByUid 通过uid得到组id
     * @xiaoyajun karl
     * @DateTime 2016-08-15T14:12:45+0800
     * @param    int                   $uid 用户id
     * @return   array                 [error_code, error_msg, group_id]
     */
    static public function getGroupIdByUid($uid)
    {
        if ($return = Db::name('auth_groups_access')->where("uid", $uid)->find()) {
            $result['error_code'] = 0;
            $result['error_msg'] = "";
            $result['group_id'] = $return['group_id'];
        } else {
            $result['error_code'] = 1;
            $result['error_msg'] = "没有对应group_id";
        }
        return $result;
    }

    /**
     * getUserRules 获取一个用户权限列表
     * @karl
     * @DateTime 2016-08-14T15:41:46+0800
     * @param    int         $uid     角色id
     * @return   array       [error_code, error_msg, data]
     */
    static public function getUserRules($uid)
    {   
        $group_info = self::getGroupIdByUid($uid);
        $rules = [];
        if($group_info['error_code'] == 0) {
            $rules = Db::name('auth_groups')->where(['group_id' => $group_info['group_id']])->find();
            $result['error_code'] = 0;
            $result['data'] = $rules;
        } else {
            return $group_info;
        }
        return $result;
    }

    /**
     * check 检查用户是否有权限
     * @彭凯 karl
     * @DateTime 2016-08-14T16:36:03+0800
     * @param    int                     $uid          用户id
     * @param    string                  $access_path  访问路径
     * @return   array                  [error_code, error_msg, data]
     */
     static public function check($uid, $access_path)
{
    $result = ['error_code' => 0, 'error_msg' => ''];
    //获取用户权限
    $rules_back = self::getUserRules($uid);
    if ($rules_back['error_code'] > 0)
        return $rules_back;
    $rules_array = explode(",", $rules_back['data']['rules']);
    if ($rules_array) {
        //判断权限
        $access_path = strtolower($access_path);
        foreach($rules_array as $value){
            $where['id'] = $value;
            $where['rules'] = ["like", "%$access_path%"];
            $rules_arr = Db::name('auth_rules')->where($where)->find();
            if (!$rules_arr) {
                $result = ['error_code' => 10, 'error_msg' => '没有些权限'];
            }else{
                $result = ['error_code' => 0, 'error_msg' => ''];
                break;
            }
        }

    }
    return  $result;
}

     //没有修改之前的check方法
     /*
        static public function check($uid, $access_path)
        {
        $result = ['error_code' => 0, 'error_msg' => ''];
        //获取用户权限
        $rules_back = self::getUserRules($uid);
        if ($rules_back['error_code'] > 0)
            return $rules_back;
        $rules_array = explode(",", $rules_back['data']['rules']);
        if ($rules_array) {
            //判断权限
            $access_path = strtolower($access_path);
            $where['rules'] = ["like", "%$access_path%"];
            $rules_array = Db::name('auth_rules')->where($where)->find();
            if (!$rules_array) {
                $result = ['error_code' => 10, 'error_msg' => '没有些权限'];
            }
        }
        return  $result;
    }
     */

    /**
     * addGroup 添加用户组
     * 操作auth_group表
     * @彭凯
     * @DateTime 2016-08-14T13:38:31+0800
     * @param    string                  $group_name 用户组名
     * @param    string                  $rules      规则
     * @return   array                   [error_code, error_msg, group_id]
     */
    static public function addGroup($group_name, $rules)
    {
        $data = ['group_name' => $group_name, 'rules' => $rules];
        $group_id = Db::name('auth_groups')->insertGetId($data);
        if ($group_id) {
            $result['error_code'] = 0;
            $result['error_msg'] = "";
            $result['group_id'] = $group_id;
        } else {
            $result['error_code'] = 1;
            $result['error_msg'] = "没有得到详情";
        }
        return $result;
    }

    /**
     * updateGroup 更新用户组
     * @karl
     * @DateTime 2017-04-06T15:28:11+0800
     * @param    int                     $group_id       用户组id
     * @param    string                  $group_name     组名称
     * @param    string                  $rules          规则
     * @return   array                   [error_code, error_msg]
     */
    static public function updateGroup($group_id, $group_name, $rules)
    {
        $data = ['group_id' => $group_id,'group_name' => $group_name,'rules' => $rules];
        $id = $data['group_id'];
        unset($data['group_id']);
        if (Db::name('auth_groups')->where(['group_id' => $id])->update($data)){
            $result['error_code'] = 0;
            $result['error_msg'] = "";
        }else{
            $result['error_code'] = 1;
            $result['error_msg'] = "更新失败";
        }
        return $result;
    }

    /**
     * deleteGroup 删除用户组
     * @彭凯
     * @DateTime 2016-08-14T16:01:46+0800
     * @param    int             $group_id                分组id
     * @return   array           [error_code, error_msg]
     */
    static public function deleteGroup($group_id)
    {
       //数据库操作后判断
       if (Db::name('auth_groups')->where(['group_id' => $group_id])->delete())
       {
           $result['error_code'] = 0;
           $result['error_msg'] = "";
       } else {
           $result['error_code'] = 1;
           $result['error_msg'] = "删除失败";
       }
       return $result;
    }
    
    /**
     * addRules 添加规则
     * @karl
     * @DateTime 2017-04-06T16:39:02+0800
     * @param    string             $title     标题
     * @param    string             $rules     规则
     * @return   array  [error_code, error_msg, id]
     */
    static public function addRules($title, $rules)
    {
        $data = ['title' => $title, 'rules' => $rules];
        $id = Db::name('auth_rules')->insertGetId($data);
        if ($id) {
            $result['error_code'] = 0;
            $result['error_msg'] = "";
            $result['id'] = $id;
        } else {
            $result['error_code'] = 1;
            $result['error_msg'] = "添加规则失败";
        }
        return $result;        
    }

    /**
     * joinGroup 用户加入用户组
     * @karl
     * @DateTime 2017-04-06T18:59:55+0800
     * @param    int             $uid           用户id
     * @param    int             $group_id      用户组id
     * @return   array           [error_code, error_msg]
     */
    static public function userJoinGroup($uid, $group_id)
    {
        if(!Db::name('auth_groups_access')->where('uid',$uid)->find()){
            $data['uid'] = $uid;
            $data['group_id'] = $group_id;
            $status = Db::name('auth_groups_access')->insert($data);
            $str = '加入用户组';
        }
        if($status){
            $return_info['error_code'] = 0;
        }else{
            $return_info['error_code'] = 1;
            $return_info['error_msg'] = $str.'失败';
        }
        return $return_info;       
    }

     /**
     * userUpdateGroup 用户更新用户组
     * @karl
     * @DateTime 2017-04-06T18:59:55+0800
     * @param    int             $uid           用户id
     * @param    int             $group_id      用户组id
     * @return   array           [error_code, error_msg]
     */
    static public function userUpdateGroup($uid, $group_id)
    {
        if(Db::name('auth_groups_access')->where('uid',$uid)->find()){
            $data['uid'] = $uid;
            $data['group_id'] = $group_id;
            if(Db::name('auth_groups_access')->where('uid',$uid)->update($data)){
                $return_info['error_code'] = 0;
            }else{
                $return_info['error_code'] = 1;
                $return_info['error_msg'] = '更新用户组失败';
            }          
        }
        return $return_info;  
    }   

}



?>